FUNCTION YBC_SWS_SENDBACK_REQUEST.
*"----------------------------------------------------------------------
*"*"本地接口：
*"  IMPORTING
*"     VALUE(IS_APPROVER) TYPE  YBC_SWS_APPROVER
*"     VALUE(IV_SENDBACKLEVEL) TYPE  YBC_SWS_APPROVER-APPLEVEL
*"     VALUE(IV_WFCONFID) TYPE  YBC_SWS_APP_CNFG-WFCONFID
*"     VALUE(IV_LANGUAGE) TYPE  CHAR10 DEFAULT 'ZH'
*"  EXPORTING
*"     VALUE(ES_MSG) TYPE  BAPIRET2
*"----------------------------------------------------------------------
  DATA:
    LS_REQUEST           TYPE YBC_SWS_REQUEST,
    LT_REQUEST           TYPE STANDARD TABLE OF YBC_SWS_REQUEST,
    LV_REMARKS           TYPE YBC_SWS_APPROVER-REMARKS,
    LV_APPLEVEL_DESC1    TYPE YBC_SWS_APPROVER-APPLEVEL_DESC1,
    LV_APPLEVEL_DESC2    TYPE YBC_SWS_APPROVER-APPLEVEL_DESC2,
    LV_APPLEVEL_DESC3    TYPE YBC_SWS_APPROVER-APPLEVEL_DESC3,
    LV_SENDBACKLEVEL_DES TYPE YBC_SWS_APPROVER-APPLEVEL_DESC1,
    LV_USERNAME          TYPE TEXT100,
    LS_APPR_USER         TYPE YBC_SWS_USER,
    LS_APPROVER          TYPE YBCS_SWS_APPROVER,
    LT_APPROVER          TYPE STANDARD TABLE OF YBCS_SWS_APPROVER,
    LT_FVALUE_H          TYPE STANDARD TABLE OF YBCS_SWS_FVALUE.

  SELECT  SINGLE *
    INTO LS_REQUEST
    FROM YBC_SWS_REQUEST
   WHERE REQUESTID = IS_APPROVER-REQUESTID
     AND MOD = GC_MOD_NEW.
  IF SY-SUBRC <> 0.
    ES_MSG-TYPE = 'E'.
    MESSAGE S021(YBC_SWS) INTO ES_MSG-MESSAGE.
    RETURN.
  ENDIF.

  SELECT SINGLE
    *
    INTO LS_APPR_USER
    FROM YBC_SWS_USER
   WHERE USERID = IS_APPROVER-APPROVERID.
  CONCATENATE LS_APPR_USER-NAME1 LS_APPR_USER-NAME2 LS_APPR_USER-NAME3 INTO LV_USERNAME SEPARATED BY SPACE.

  CONDENSE LV_USERNAME.

  IF IV_SENDBACKLEVEL = 'RETURN'.
    MESSAGE S032(YBC_SWS) WITH LV_USERNAME INTO LV_REMARKS.
    LS_REQUEST-STATUS = GC_STATUS_RETURNED.
    LS_REQUEST-MOD = GC_MOD_RETURN.
    LS_REQUEST-MESSAGE = LV_REMARKS .

    MODIFY YBC_SWS_REQUEST FROM LS_REQUEST.
  ELSE.

    IF IV_SENDBACKLEVEL IS INITIAL.
      ES_MSG-TYPE = 'E'.
      MESSAGE S038(YBC_SWS) INTO ES_MSG-MESSAGE.
      RETURN.
    ENDIF.

    SELECT *
        INTO CORRESPONDING FIELDS OF TABLE LT_FVALUE_H
        FROM YBC_SWS_REQHEAD
       WHERE REQUESTID = IS_APPROVER-REQUESTID.

    SELECT SINGLE
      APPLEVEL_DESC1
      APPLEVEL_DESC2
      APPLEVEL_DESC3
      INTO ( LV_APPLEVEL_DESC1, LV_APPLEVEL_DESC2, LV_APPLEVEL_DESC3 )
      FROM YBC_SWS_APPROVER
     WHERE REQUESTID = IS_APPROVER-REQUESTID
       AND APPLEVEL = IV_SENDBACKLEVEL.

    TRANSLATE IV_LANGUAGE TO UPPER CASE.
    CASE IV_LANGUAGE.
      WHEN 'ZH'.
        LV_SENDBACKLEVEL_DES = LV_APPLEVEL_DESC1.
      WHEN 'EN'.
        LV_SENDBACKLEVEL_DES = LV_APPLEVEL_DESC2.
      WHEN OTHERS.
        LV_SENDBACKLEVEL_DES = LV_APPLEVEL_DESC3.
    ENDCASE.

    CLEAR ES_MSG.
    CALL FUNCTION 'YBC_SWS_GET_APPROVER'
      EXPORTING
        IV_WFCONFID   = IV_WFCONFID
        IV_APPLEVEL   = IV_SENDBACKLEVEL
*       IV_FIRST_LEVEL       =
      IMPORTING
        ES_MSG        = ES_MSG
      TABLES
        IT_REQ_HEADER = LT_FVALUE_H
        ET_APPROVER   = LT_APPROVER.

    IF ES_MSG-TYPE = 'E'.
      RETURN.
    ENDIF.

    LOOP AT LT_APPROVER INTO LS_APPROVER.
      LS_APPROVER-VERSION = IS_APPROVER-VERSION + 1.
      MODIFY LT_APPROVER FROM LS_APPROVER.
    ENDLOOP.

    CLEAR ES_MSG.
    CALL FUNCTION 'YBC_SWS_SEND_NEXT'
      EXPORTING
        IV_REQUESTID = IS_APPROVER-REQUESTID
      IMPORTING
        ES_MSG       = ES_MSG
      TABLES
        IT_APPROVER  = LT_APPROVER.
    IF ES_MSG-TYPE = 'E'.
      RETURN.
    ENDIF.

    "Remarks 更新，只更新还未处理的审批级别
    MESSAGE S039(YBC_SWS) WITH LV_USERNAME LV_SENDBACKLEVEL_DES INTO LV_REMARKS.

  ENDIF.

  "更新实际处理标识
  UPDATE YBC_SWS_APPROVER
      SET ACTION = 'SENDBACK'
          COMMENTS = IS_APPROVER-COMMENTS
          REMARKS = LV_REMARKS
          ACTUALPROCESSED = 'X'
          ADATE = SY-DATUM
          ATIME = SY-UZEIT
    WHERE REQUESTID = IS_APPROVER-REQUESTID
      AND APPROVERID = IS_APPROVER-APPROVERID
      AND APPLEVEL = IS_APPROVER-APPLEVEL
      AND VERSION = IS_APPROVER-VERSION
      AND PROCESSED = SPACE.


  UPDATE YBC_SWS_APPROVER
    SET CANCELED = 'X'
        PROCESSED = 'X'
  WHERE REQUESTID = IS_APPROVER-REQUESTID
    AND VERSION = IS_APPROVER-VERSION.
  " AND PROCESSED = SPACE.

  COMMIT WORK AND WAIT.
  IF SY-SUBRC <> 0.
    ROLLBACK WORK.
    RETURN.
  ENDIF.

  ES_MSG-TYPE = 'S'.
  MESSAGE S032(YBC_SWS) WITH SY-UNAME INTO ES_MSG-MESSAGE.
ENDFUNCTION.
